package com.bt.lms.dao.servise.impl;

import com.bt.lms.bo.QueryCourseCondition;
import com.bt.lms.constant.BOConstant;
import com.bt.lms.entity.CourseInfo;
import com.bt.lms.dao.servise.CourseInfoDao;
import com.bt.lms.entity.page.Page;
import com.bt.lms.utils.ConnectUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 课程信息Dao实现
 *
 * @author 李鹏
 */
public class CourseInfoDaoImpl implements CourseInfoDao {

    @Override
    public int insert(String sql, CourseInfo courseInfo) throws Exception {

        //需要维护的属性
        courseInfo.setStatus(BOConstant.COURSE_INFO_STATUS_NOT_START);
        //准备操作数据库
        //课程信息表
        //课程编号
        UUID uuid = UUID.randomUUID();
        String code = uuid.toString();
        courseInfo.setCode(code);

        String courseName = courseInfo.getName();
        String englishName = courseInfo.getEnglishName();
        Integer studyCenter = courseInfo.getStudyCenter();
        Integer applyLevel = courseInfo.getApplyLevel();
        String versions = courseInfo.getVersions();

        //jdbc,执行sql语句，知道插入的课程信息ID
        //jdbc五步
        Connection connection = ConnectUtils.connect();
        //4.执行sql语句
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1, code);
        ps.setString(2, courseName);
        ps.setString(3, englishName);
        ps.setInt(4, studyCenter);
        ps.setInt(5, applyLevel);
        ps.setString(6, versions);

        int i = ps.executeUpdate();
        //关资源
        ps.close();
        connection.close();

        return i;
    }

    @Override
    public CourseInfo select(String sql, Long id) throws Exception {

        //jdbc执行sql语句，知道课程的id
        CourseInfo courseInfo = new CourseInfo();
        //jdbc五大步
        Connection connection = ConnectUtils.connect();
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setLong(1, id);
        //4.执行sql语句
        ResultSet resultSet = ps.executeQuery();
        boolean next = resultSet.next();
        if (next) {

            long id1 = resultSet.getLong("id");
            String code = resultSet.getString("code");
            String courseName = resultSet.getString("course_name");
            String englishName = resultSet.getString("english_name");
            int studyCenter = resultSet.getInt("study_center");
            int applyLevel = resultSet.getInt("apply_level");
            int versions = resultSet.getInt("versions");
            //带走
            courseInfo.setId(id1);
            courseInfo.setCode(code);
            courseInfo.setName(courseName);
            courseInfo.setEnglishName(englishName);
            courseInfo.setStudyCenter(studyCenter);
            courseInfo.setApplyLevel(applyLevel);
            //int转String
            String versionStr = Integer.toString(versions);
            courseInfo.setVersions(versionStr);

            ps.close();
            connection.close();
        }

        return courseInfo;
    }

    @Override
    public boolean update(String sql, Long id, CourseInfo courseInfo) throws Exception {

        //需要维护的属性
        courseInfo.setStatus(BOConstant.COURSE_INFO_STATUS_NOT_START);
        //准备操作数据库
        //课程信息表
        //课程编号
        UUID uuid = UUID.randomUUID();
        String code = uuid.toString();
        courseInfo.setCode(code);

        String courseName = courseInfo.getName();
        String englishName = courseInfo.getEnglishName();
        Integer studyCenter = courseInfo.getStudyCenter();
        Integer applyLevel = courseInfo.getApplyLevel();
        String versions = courseInfo.getVersions();

        //jdbc,执行sql语句，知道插入的课程信息ID
        //jdbc五步
        Connection connection = ConnectUtils.connect();
        //4.执行sql语句
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setString(1, code);
        ps.setString(2, courseName);
        ps.setString(3, englishName);
        ps.setInt(4, studyCenter);
        ps.setInt(5, applyLevel);
        ps.setString(6, versions);
        ps.setLong(7, id);

        int i = ps.executeUpdate();
        //关资源
        ps.close();
        connection.close();

        return false;
    }

    @Override
    public boolean delete(String sql, Long id) throws Exception {

        //jdbc,执行sql语句，知道插入的课程信息ID
        //jdbc五步
        Connection connection = ConnectUtils.connect();
        //4.执行sql语句
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setLong(1, id);
        //执行
        ps.executeUpdate();

        return false;
    }

    @Override
    public boolean updateStart(String sql, Long id) throws Exception {

        //jdbc,执行sql语句，知道插入的课程信息ID
        //jdbc五步
        Connection connection = ConnectUtils.connect();
        //4.执行sql语句
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setLong(1, id);
        //执行
        ps.executeUpdate();

        return false;
    }

    @Override
    public boolean updateStop(String sql, Long id) throws Exception {

        //jdbc,执行sql语句，知道插入的课程信息ID
        //jdbc五步
        Connection connection = ConnectUtils.connect();
        //4.执行sql语句
        PreparedStatement ps = connection.prepareStatement(sql);
        ps.setLong(1, id);
        //执行
        ps.executeUpdate();

        return false;

    }

    @Override
    public boolean addAndUpdateStart(String addSql, String updateSql, CourseInfo courseInfo, Long id) throws Exception {

        //添加，需要维护的属性
        courseInfo.setStatus(BOConstant.COURSE_INFO_STATUS_NOT_START);
        //准备操作数据库
        //课程信息表
        //课程编号
        UUID uuid = UUID.randomUUID();
        String code = uuid.toString();
        courseInfo.setCode(code);

        String courseName = courseInfo.getName();
        String englishName = courseInfo.getEnglishName();
        Integer studyCenter = courseInfo.getStudyCenter();
        Integer applyLevel = courseInfo.getApplyLevel();
        String versions = courseInfo.getVersions();

        //jdbc,执行sql语句，知道插入的课程信息ID
        //jdbc五步
        Connection connection = ConnectUtils.connect();
        //4.执行sql语句
        PreparedStatement ps = connection.prepareStatement(addSql);
        ps.setString(1, code);
        ps.setString(2, courseName);
        ps.setString(3, englishName);
        ps.setInt(4, studyCenter);
        ps.setInt(5, applyLevel);
        ps.setString(6, versions);
        int i = ps.executeUpdate();

        //启用，jdbc,执行sql语句，知道插入的课程信息ID
        //jdbc五步
        Connection connection1 = ConnectUtils.connect();
        //4.执行sql语句
        PreparedStatement ps1 = connection1.prepareStatement(updateSql);
        ps1.setLong(1, id);
        //执行
        ps1.executeUpdate();

        return false;
    }

    @Override
    public Page<CourseInfo> pageQuery(String sql, Page<CourseInfo> page) {

        //分页条件模型
        List<CourseInfo> courseInfos = new ArrayList<>();
        //jdbc五步
        Connection connection = ConnectUtils.connect();
        //4.执行sql语句
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            Integer current = page.getCurrent(); //当前页
            Integer size = page.getSize(); //显示数量
            int startSize = size * (current - 1); //开始条数
            ps.setInt(1, startSize);
            ps.setInt(2, size);
            ResultSet resultSet = ps.executeQuery();
            int i = 0;
            while (resultSet.next()) {
                long id1 = resultSet.getLong("id");
                String code = resultSet.getString("code");
                String courseName = resultSet.getString("course_name");
                String englishName = resultSet.getString("english_name");
                int studyCenter = resultSet.getInt("study_center");
                int applyLevel = resultSet.getInt("apply_level");
                String versions = resultSet.getString("versions");
                int status = resultSet.getInt("status");

                CourseInfo courseInfo = new CourseInfo();
                courseInfo.setId(id1);
                courseInfo.setCode(code);
                courseInfo.setName(courseName);
                courseInfo.setEnglishName(englishName);
                courseInfo.setStudyCenter(studyCenter);
                courseInfo.setApplyLevel(applyLevel);
                courseInfo.setVersions(versions);
                courseInfo.setStatus(status);
                courseInfos.add(courseInfo);
            }
            page.setRecords(courseInfos);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return page;
    }
}
